//
//  SettingTimeController.m
//  YSSmartHome
//
//  Created by yanghao on 16/9/19.
//  Copyright © 2016年 yahaong. All rights reserved.
//

#import "SettingTimeController.h"
#import "SureView.h"
#import "RepeatDateController.h"
#import "RepeatDateModel.h"
#import "SelectSpecificController.h"
#import "ZTLinkageUtil.h"
#import "StartConditionController.h"
#import "SceneEditController.h"

@interface SettingTimeController ()
<
UITableViewDelegate,
UITableViewDataSource
>
@property (nonatomic, strong)UIView *datePickerBgView;
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)SureView *sureView;
@property (nonatomic, strong)NSArray *wdays;
@property (nonatomic, strong)UIDatePicker *datePicker;
@end

@implementation SettingTimeController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"设定时间";
    [self.view addSubview:self.datePickerBgView];
    [self.datePickerBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.trailing.leading.equalTo(self.view);
        make.height.equalTo(200);
    }];
    UIDatePicker *datePicker = [[UIDatePicker  alloc]init];
    datePicker.datePickerMode =  UIDatePickerModeTime;

    datePicker.locale = [[NSLocale alloc]initWithLocaleIdentifier:@"zh_CN"];
    [datePicker addTarget:self action:@selector(dateChange:) forControlEvents:UIControlEventValueChanged];
    self.datePicker = datePicker;
    [self.datePickerBgView addSubview:datePicker];
    [datePicker mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.datePickerBgView);
    }];
    [self.view addSubview:self.sureView];
    [self.sureView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.bottom.equalTo(self.view);
        make.height.equalTo(110);
    }];
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.datePickerBgView.bottom).offset(10);
        make.leading.trailing.equalTo(self.view);
        make.bottom.equalTo(self.sureView.top);
    }];
    __weak typeof(self) weakSelf = self;
    self.sureView.sureBtnClickBlock = ^() {
        __block  __strong typeof(weakSelf)strongSelf = weakSelf;
        if (!strongSelf)
        {
            return ;
        }
        [strongSelf setLinkageTime];
    };
}

- (void)dateChange:(UIDatePicker *)datePicker
{
    NSDateFormatter *fmt = [[NSDateFormatter alloc]init];
    fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    NSString *dateStr = [fmt stringFromDate:datePicker.date];
    debugLogInfo(@"dateStr = %@",dateStr);
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)setLinkageTime
{   
    LinkageTimeStruct *timeStruct = [[LinkageTimeStruct alloc]init];
    [timeStruct setDate:self.datePicker.date wdays:nil dimension:Linkage_Time_Dimension_All];
    
    debugLogInfo(@"timeStruct = %@",[timeStruct toStr]);
    XAILinkItemDes * linkItemDes = [[XAILinkItemDes alloc]init];
    XAITLV *tlv = [[XAITLV alloc]init];
    tlv.type = XAI_DATA_TYPE_BIN_DATE;
    [tlv setPayloadbyte:[timeStruct getBinary] withSize:19];
    linkItemDes.tlv = tlv;
    linkItemDes.apsn = 0;
    linkItemDes.luid = 0;
    linkItemDes.oprId = 0;
    ZTLinkageInfo *linkageInfo = [[ZTLinkageInfo alloc]init];
    linkageInfo.strSelectDesc =  nil;
    linkageInfo.strNormalDesc = nil;
    linkageInfo.linkItemDes = linkItemDes;
    linkageInfo.linkageType  = self.linkageType;
    if (linkageInfo)
    {
        [[NSNotificationCenter defaultCenter]postNotificationName:@"kZTAddLinkageInfoNotifKey" object:@[linkageInfo]];
    }
    Class class = [StartConditionController class];
    if (self.isEdit)
    {
        class = [SceneEditController class];
    }
    for (UIViewController *temp in self.navigationController.viewControllers)
    {
        if ([temp isKindOfClass:class])
        {
            [self.navigationController popToViewController:temp animated:YES];
        }
    }

}

#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    if (indexPath.row == 0) {
        cell.textLabel.text = @"重复日期";
        cell.detailTextLabel.text = [self allString:self.wdays];
    }else if (indexPath.row == 1) {
        cell.textLabel.text = @"选择特定日期";
    }
    return cell;
}

- (NSString *)allString:(NSArray *)repeateDates
{
    NSMutableString *str = [NSMutableString string];
    for (RepeatDateModel *repeateDate in self.wdays) {
        [str appendFormat:@"%@  ",repeateDate.title];
    }
    return str;
}
#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        RepeatDateController *repeatDateVC = [[RepeatDateController alloc]init];
        __weak typeof(self) weakSelf = self;
        repeatDateVC.repeatDateBlock = ^(NSArray *repeateDates) {
            __block  __strong typeof(weakSelf)strongSelf = weakSelf;
            if (!strongSelf) {
                return ;
            }
            strongSelf.wdays = repeateDates;
            [strongSelf.tableView reloadData];
        };
        [self.navigationController pushViewController:repeatDateVC animated:YES];
    } else if (indexPath.row == 1) {
        SelectSpecificController *selectSpecificVc = [[SelectSpecificController alloc]init];
        [self.navigationController pushViewController:selectSpecificVc animated:YES];
    }
}
- (UIView *)datePickerBgView
{
    if (!_datePickerBgView) {
        _datePickerBgView = [[UIView alloc]init];
        _datePickerBgView.backgroundColor = [UIColor whiteColor];
    }
    return _datePickerBgView;
}
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]init];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = kBgColor;
        _tableView.rowHeight = 55;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.tableFooterView = [[UIView alloc]init];
        _tableView.bounces = NO;    
    }
    return _tableView;
}

- (SureView *)sureView
{
    if (!_sureView) {
        _sureView = [[SureView alloc]init];
    }
    return _sureView;
}
@end
